<template>
  <div class="return-stock-in-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>退货入库</span>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="退货单号">
          <el-input v-model="queryForm.returnNo" placeholder="请输入退货单号" />
        </el-form-item>
        <el-form-item label="客户">
          <el-select
            v-model="queryForm.customer"
            filterable
            remote
            placeholder="请选择客户"
          >
            <el-option
              v-for="item in customerOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="退货日期">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryForm.status" placeholder="请选择状态">
            <el-option label="待检验" value="pending" />
            <el-option label="待入库" value="toBeStored" />
            <el-option label="已入库" value="completed" />
            <el-option label="已拒收" value="rejected" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="expand">
          <template #default="props">
            <el-table :data="props.row.items" border>
              <el-table-column prop="productCode" label="产品编码" />
              <el-table-column prop="productName" label="产品名称" />
              <el-table-column prop="specification" label="规格型号" />
              <el-table-column prop="returnQuantity" label="退货数量" />
              <el-table-column prop="inspectedQuantity" label="已检数量" />
              <el-table-column prop="qualifiedQuantity" label="合格数量" />
              <el-table-column prop="stockInQuantity" label="已入库数量" />
              <el-table-column
                prop="returnReason"
                label="退货原因"
                show-overflow-tooltip
              />
            </el-table>
          </template>
        </el-table-column>
        <el-table-column prop="returnNo" label="退货单号" width="180" />
        <el-table-column prop="customerName" label="客户" />
        <el-table-column prop="returnDate" label="退货日期" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleInspect(scope.row)"
              v-if="scope.row.status === 'pending'"
              >检验</el-button
            >
            <el-button
              link
              type="primary"
              @click="handleStockIn(scope.row)"
              v-if="scope.row.status === 'toBeStored'"
              >入库</el-button
            >
            <el-button
              link
              type="danger"
              @click="handleReject(scope.row)"
              v-if="scope.row.status === 'pending'"
              >拒收</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 检验对话框 -->
    <el-dialog v-model="inspectDialogVisible" title="退货检验" width="80%">
      <el-form :model="inspectForm" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="退货单号">
              <el-input v-model="inspectForm.returnNo" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户">
              <el-input v-model="inspectForm.customerName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检验人" required>
              <el-input
                v-model="inspectForm.inspector"
                placeholder="请输入检验人"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-table :data="inspectForm.items" border>
          <el-table-column prop="productCode" label="产品编码" />
          <el-table-column prop="productName" label="产品名称" />
          <el-table-column prop="specification" label="规格型号" />
          <el-table-column prop="returnQuantity" label="退货数量" />
          <el-table-column label="合格数量" width="150">
            <template #default="scope">
              <el-input-number
                v-model="scope.row.qualifiedQuantity"
                :min="0"
                :max="scope.row.returnQuantity"
              />
            </template>
          </el-table-column>
          <el-table-column label="不合格原因" width="200">
            <template #default="scope">
              <el-input
                v-model="scope.row.unqualifiedReason"
                :disabled="
                  scope.row.qualifiedQuantity === scope.row.returnQuantity
                "
                placeholder="请输入不合格原因"
              />
            </template>
          </el-table-column>
          <el-table-column label="处理意见" width="200">
            <template #default="scope">
              <el-input
                v-model="scope.row.handleSuggestion"
                placeholder="请输入处理意见"
              />
            </template>
          </el-table-column>
        </el-table>

        <el-form-item label="检验备注" class="mt-20">
          <el-input v-model="inspectForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="inspectDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleInspectSubmit"
            >确认</el-button
          >
        </div>
      </template>
    </el-dialog>

    <!-- 入库对话框 -->
    <el-dialog v-model="stockInDialogVisible" title="退货入库" width="80%">
      <el-form :model="stockInForm" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="退货单号">
              <el-input v-model="stockInForm.returnNo" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户">
              <el-input v-model="stockInForm.customerName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入库仓库" required>
              <el-select
                v-model="stockInForm.warehouse"
                placeholder="请选择仓库"
              >
                <el-option
                  v-for="item in warehouseOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-table :data="stockInForm.items" border>
          <el-table-column prop="productCode" label="产品编码" />
          <el-table-column prop="productName" label="产品名称" />
          <el-table-column prop="specification" label="规格型号" />
          <el-table-column prop="qualifiedQuantity" label="合格数量" />
          <el-table-column prop="stockInQuantity" label="已入库数量" />
          <el-table-column label="本次入库数量" width="150">
            <template #default="scope">
              <el-input-number
                v-model="scope.row.currentQuantity"
                :min="0"
                :max="scope.row.qualifiedQuantity - scope.row.stockInQuantity"
              />
            </template>
          </el-table-column>
          <el-table-column label="库位" width="150">
            <template #default="scope">
              <el-select v-model="scope.row.location" placeholder="请选择库位">
                <el-option
                  v-for="item in locationOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="备注" width="200">
            <template #default="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>

        <el-form-item label="入库备注" class="mt-20">
          <el-input v-model="stockInForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="stockInDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleStockInSubmit"
            >确认入库</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

// ... 省略状态定义 ...

const handleInspectSubmit = () => {
  if (!inspectForm.inspector) {
    ElMessage.warning("请输入检验人");
    return;
  }

  // 验证数据
  const valid = inspectForm.items.every((item) => {
    if (
      item.qualifiedQuantity < item.returnQuantity &&
      !item.unqualifiedReason
    ) {
      ElMessage.warning(`${item.productName} 请输入不合格原因`);
      return false;
    }
    return true;
  });

  if (!valid) return;

  // 实现检验逻辑
  ElMessage.success("检验完成");
  inspectDialogVisible.value = false;
};

const handleStockInSubmit = () => {
  if (!stockInForm.warehouse) {
    ElMessage.warning("请选择入库仓库");
    return;
  }

  // 验证数据
  const valid = stockInForm.items.every((item) => {
    if (item.currentQuantity > 0 && !item.location) {
      ElMessage.warning(`${item.productName} 请选择库位`);
      return false;
    }
    return true;
  });

  if (!valid) return;

  // 验证是否有入库数量
  const hasQuantity = stockInForm.items.some(
    (item) => item.currentQuantity > 0
  );
  if (!hasQuantity) {
    ElMessage.warning("请输入入库数量");
    return;
  }

  // 实现入库逻辑
  ElMessage.success("入库成功");
  stockInDialogVisible.value = false;
};

const handleReject = (row) => {
  ElMessageBox.confirm("确认拒收该退货单？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    ElMessage.success("已拒收");
  });
};
</script>

<style scoped>
.return-stock-in-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.mt-20 {
  margin-top: 20px;
}
</style>
